/*
 * Copyright 2009 Johnson Consulting Services
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
/**
 * 
 */
package com.johnsoncs.gwt.widgets.samples.server.mock;

import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;

import com.johnsoncs.gwt.widgets.client.dynamictable.export.ExportException;
import com.johnsoncs.gwt.widgets.client.dynamictable.export.ExportRequest;
import com.johnsoncs.gwt.widgets.client.dynamictable.export.ExportRuntimeException;
import com.johnsoncs.gwt.widgets.client.util.Callback;
import com.johnsoncs.gwt.widgets.samples.client.mock.MyRowObject;
import com.johnsoncs.gwt.widgets.server.export.ExportFormat;
import com.johnsoncs.gwt.widgets.server.export.ExportModel;
import com.johnsoncs.gwt.widgets.server.export.ExportStorage;

/**
 * This is an example class showing how to use a raw export format, rather than a mapped one.
 * It's overly simple, but since the built-in formats are all MappedExportFormats, this makes a good demo of the base ExportFormat interface.
 * @author wilj
 *
 */
public class MyRowObjectHtmlExporter implements ExportFormat<MyRowObject> {

	/* (non-Javadoc)
	 * @see com.johnsoncs.gwt.widgets.server.export.ExportFormat#export(java.lang.String, com.johnsoncs.gwt.widgets.client.dynamictable.export.ExportRequest, com.johnsoncs.gwt.widgets.server.export.ExportModel, com.johnsoncs.gwt.widgets.server.export.ExportStorage)
	 */
	@Override
	public String export(final String exportId, final ExportRequest<MyRowObject> request,
			final ExportModel<MyRowObject> model,
			final ExportStorage storage) throws ExportException {
		try {
			final String fileName = "export" + exportId + "." + getFileExtension();
			final OutputStream out = storage.initExportResult(fileName);
			final Writer w = new OutputStreamWriter(out);

			try {
				w.write("<html><body><table><tr><td>ID</td><td>First Name</td><td>Last Name</td><td>Status</td></tr>");
				model.export(request, new Callback<MyRowObject>() {
					@Override
					public void exec(final MyRowObject value) {
						final String status = (value.isStatus()) ? "OK" : "WARN";

						try {
							w.write("<tr><td>" + value.getId() + "</td><td>" + value.getFirstName() + "</td><td>"
									+ value.getLastName() + "</td><td>" + status + "</td></tr>");
						} catch (final IOException e) {
							throw new ExportRuntimeException("Error writing to file", e);
						}
					}
				});
				w.write("</table></body></html>");

			} finally {
				w.flush();
				w.close();
				storage.finalizeExportResult(fileName);
			}
			return fileName;

		} catch (final IOException e) {
			throw new ExportException("Error exporting file", e);
		}
	}

	/* (non-Javadoc)
	 * @see com.johnsoncs.gwt.widgets.server.export.ExportFormat#getFileExtension()
	 */
	@Override
	public String getFileExtension() {
		return "html";
	}

	/* (non-Javadoc)
	 * @see com.johnsoncs.gwt.widgets.server.export.ExportFormat#getMimeType()
	 */
	@Override
	public String getMimeType() {
		return "text/html";
	}

	/* (non-Javadoc)
	 * @see com.johnsoncs.gwt.widgets.server.export.ExportFormat#isAttachment()
	 */
	@Override
	public boolean isAttachment() {
		return false;
	}

}
